package com.stonepuzzle;

import javax.swing.*;
import java.awt.event.*;
import java.util.Random;

public class MainFrame extends JFrame implements KeyListener {
    int[][] date={
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0},
    };

    int[][] win={
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0},
    };

    int row; // 0号元素行坐标位置
    int column; // 0号元素列坐标位置

    int count; //计步器

    public MainFrame(){
        // 窗体对象.addKeyListener(keyListener实现类对象)
        this.addKeyListener(this);
        // 初始化窗体
        initFrame();
        // 初始化数据（打乱石头）
        initDate();
        // 绘制游戏界面
        paintView();
        // 设置窗体可见
        setVisible(true);
    }



    /**
     * 窗体初始化
     */
    public void initFrame(){
        setSize(514,595);
        // 设置关闭模式
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("石头迷阵");
        // 设置窗体置顶
        setAlwaysOnTop(true);
        // 设置窗体居中
        setLocationRelativeTo(null);
        // 取消默认布局
        setLayout(null);
    }

    /**
     * // 初始化数据（打乱石头）
     */
    private void initDate() {
        Random r=new Random();
        for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                int randomX = r.nextInt(4);
                int randomY = r.nextInt(4);
                int temp=date[i][j];
                date[i][j]=date[randomX][randomY];
                date[randomX][randomY]=temp;
            }
        }

        for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                if(date[i][j]==0){
                    row=i;
                    column=j;
                }
            }
        }
    }

    /**
     * 判断是否胜利
     */
    public boolean victory(){
        for (int i = 0; i < date.length; i++) {
            for (int j = 0; j < date[i].length; j++) {
                if(date[i][j]!=win[i][j]){
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 绘制游戏界面
     */
    public void paintView(){
        getContentPane().removeAll();
        if (victory()){
            JLabel imageWin=new JLabel(new ImageIcon("D:\\Develop\\hou-duan-study\\image\\win.png"));
            imageWin.setBounds(124,230,266,88);
            getContentPane().add(imageWin);
        }

        JLabel countLabel=new JLabel("步数为:"+count);
        countLabel.setBounds(50,20,100,20);
        getContentPane().add(countLabel);

        JButton btn=new JButton("重新开始");
        btn.setBounds(350,20,100,20);
        getContentPane().add(btn);
        btn.setFocusable(false);
//        btn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                count=0;
//                initDate();
//                paintView();
//            }
//        });
        btn.addActionListener(e -> {
            count=0;
            initDate();
            paintView();
        });




        for(int i=0;i<4;i++) {
            for (int j = 0; j < 4; j++) {
                JLabel imageLabel = new JLabel(new ImageIcon("D:\\Develop\\hou-duan-study\\image\\" + date[i][j] + ".png"));
                imageLabel.setBounds(50 + 100 * j, 90 + 100 * i, 100, 100);
                getContentPane().add(imageLabel);
            }
        }
        JLabel backgbround=new JLabel(new ImageIcon("D:\\Develop\\hou-duan-study\\image\\background.png"));
        backgbround.setBounds(26,30,450,484);
        getContentPane().add(backgbround);
        getContentPane().repaint();
    }

    /**
     * 键盘监听
     */
    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode=e.getKeyCode();
        move(keyCode);
        paintView();
    }

    /**
     * 移动业务
     */
    private void move(int keyCode) {
        if(victory()){
            return;
        }
        if(keyCode ==37){
            if(column==3){
                return;
            }
            System.out.println("左业务");
            int temp=date[row][column];
            date[row][column]=date[row][column+1];
            date[row][column+1]=temp;
            column++;
            count++;
        }else if(keyCode ==38){
            if(row==3){
                return;
            }
            System.out.println("上业务");
            int temp=date[row][column];
            date[row][column]=date[row+1][column];
            date[row+1][column]=temp;
            row++;
            count++;
        }else if(keyCode ==39){
            if(column==0){
                return;
            }
            System.out.println("右业务");
            int temp=date[row][column];
            date[row][column]=date[row][column-1];
            date[row][column-1]=temp;
            column--;
            count++;
        }else if(keyCode ==40){
            if(row==0){
                return;
            }
            System.out.println("下业务");
            int temp=date[row][column];
            date[row][column]=date[row-1][column];
            date[row-1][column]=temp;
            row--;
            count++;
        }else if(keyCode==90){
            // 按z触发作弊器
            date=new int[][]{
                    {1,2,3,4},
                    {5,6,7,8},
                    {9,10,11,12},
                    {13,14,15,0},
            };

        }
    }

    //----------------------------------------------
    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {
    }
    //----------------------------------------------
}
